//保留小数点后四位，不足的补0
function toDecimal4(v){
    var f=parseFloat(v);
    if(isNaN(f)){
        return NaN;
    }
    var f = Math.round(v*10000)/10000;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 4) {
        s += '0';
    }
    return s;

}


function index_handregister(){
    //净值日期
    Handlebars.registerHelper('fmtdate', function(v,options) {
        if(v.length<6){
            return "--";
        }
        var year=v.substring(0,4);
        var month=v.substring(4,6);
        var day=v.substring(6);
        return year+"-"+month+"-"+day;
    });
    //基金净值-非货币（包括理财）：保留小数点后四位
    Handlebars.registerHelper('fmtfhbnav', function(v,options) {

        var snav=toDecimal4(v);
        if(isNaN(snav)){
            return "--";
        }else{
            return snav;
        }
    });


    //判断是否为空
    Handlebars.registerHelper('isnull', function(v, options) {
        if(v==null){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }
    });

    Handlebars.registerHelper('isred', function(v1, options) {
        if(parseFloat(v1)>=0){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }

    });
    //是否是认购期
    Handlebars.registerHelper('isrgq', function(transaction, options) {
        if(transaction==10){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }
    });
    //是否能认购，ETF不能认购
    Handlebars.registerHelper('canrg', function(status, options) {
        if(status!=4){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }

    });
    //6:表示暂停赎回，可以申购
    Handlebars.registerHelper('cansg', function(zx_status,options) {
        if(zx_status=="0" || zx_status=='6'){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }

    });

    Handlebars.registerHelper('isShowYzqsyl', function(v, options) {
        if(v!=null && v!=0){
            return options.fn(this);
        }else{
            return options.inverse(this);
        }

    });
}
//渲染模板
function xrtemplate(tempalteid,tableid,data){
    index_handregister();
    var mTemplate=Handlebars.compile($("#"+tempalteid).html());
    $("#"+tableid).html(mTemplate(data));
    sortIndexTableById(tableid);
}

/*****表排序[start]********/
function sortIndexTableById(id){
    $("#"+id).tablesorter(
        {
            sortInitialOrder: "desc",
            headers:{6:{sorter:"percent"},7:{sorter:"percent"},8:{sorter:"percent"}}
        }
    );
}
/*****表排序[end]********/



//初始化首页基金数据
function initIndexPage(){
/*    xrtemplate("template_index_fhb","gpid",g_index_gplist);//股票
    xrtemplate("template_index_fhb","hhid",g_index_hhlist);//混合
    xrtemplate("template_index_fhb","zqid",g_index_zqlist);//债券
    xrtemplate("template_index_fhb","bbid",g_index_bblist);//保本
    xrtemplate("template_index_fhb","zsid",g_index_zslist);//指数
    xrtemplate("template_index_hb","hbid",g_index_hblist);//货币
    xrtemplate("template_index_lcj","lcjid",g_index_lcjlist);//理财金H
    xrtemplate("template_index_lc","lcid",g_index_lclist);//理财
    xrtemplate("template_index_fhb","qdid",g_index_qdlist);//QD
    xrtemplate("template_index_fhb","fofid",g_index_foflist);//FOF
    xrtemplate("template_index_fhb","hrid",g_index_hrlist);//互认
*/}




//**********初始化start************
initIndexPage();

//**********初始化end************